import '@/css/base.css';


import Vue from 'vue'
import App from '@/App.vue'


import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';


import router from '@/router.js'
import store from '@/store/store.js'

import { capitalize } from '@/utils/filter.js'



import "@/css/iconfont/iconfont.css";

import Fragment from 'vue-fragment'
Vue.use(Fragment.Plugin)

import  cfg from '@/config/config.js'
Vue.prototype.$config = cfg

import  docCookies from '@/utils/do-cookie.js'
Vue.prototype.$docCookies = docCookies

import  api from '@/config/api.js'
Vue.prototype.$api = api

import axios from 'axios'
axios.defaults.baseURL = 'http://localhost:3000';
axios.defaults.timeout = 6000;
axios.interceptors.request.use((config) => { 
    console.log('触发于vue的main.js,拦截器axios.interceptors.request')
    var jwtStr = ''
  //登录时设置cookie,发请求时考虑到跨域,把cookie放到header里
    jwtStr=docCookies.getItem(cfg.COOKIEJWTNAME)
    config.headers = {
        'access-token': jwtStr
    }
    return config
}, (err) => {}
)

axios.interceptors.response.use((config) => { 
   //只有状态码是200才会进入这里,否则就去下方的err函数
    if (config.data.errCode === -401) { 
        //-401无权限
        router.replace({path:'/admin'})
        return
    }
        return config.data
}, (err) => { 
        alert('from vue main.js\n'+'axios.interceptors.response.use'+'\n'+err)
})

Vue.prototype.$axios = axios

// alert(process.env.NODE_ENV).




Vue.use(ElementUI);


// 注册为全局过滤器
Vue.filter('Capitalize', capitalize)

Vue.config.productionTip = false

//compiler
/* new Vue({
    el: '#app',
    store:store,
    router: router,
    template: '<App/>',
    components: { App }
  }) */
  router.beforeEach((to, from, next) => {
      console.log('--main.js--',to)
      if (to.matched.some(record => record.meta.requireAuth)) {
          next({
              name: 'admin',
              query: { redirect: to.fullPath }
          })
      } else { 
        next()
      }
     
  })

//runtime
new Vue({
    router,
    store,
    render: h => h(App)
  }).$mount("#app")